//
//  StateRecordViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/8/3.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "StateRecordViewController.h"
#import "StateRecordTableView.h"
#import "StateHeaderView.h"
#import "StatusRecordModel.h"

//static NSString *StateCellIdentifier = @"StateRecordCell";

@interface StateRecordViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *array;

@end

@implementation StateRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"交付记录"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.array = @[@[@"平台已更新",@"2015-07-02"],@[@"待定款",@"2015-07-02"],@[@"已完工",@"2015-07-02"],@[@"开发中",@"2015-07-02"],@[@"待付款",@"2015-07-02"],@[@"提交定制单",@"2015-07-02"]];
//    [self.myTableView registerNib:[UINib nibWithNibName:@"StateRecordCell" bundle:nil]
//           forCellReuseIdentifier:StateCellIdentifier];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"orderId":self.model.orderId==nil?@"":self.model.orderId};
    [RequestData queryDeliveryRecords:[UIUtil toJsonString:dic] withBlock:^(NSArray *array, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.array = array;
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        StateHeaderView *view = [[StateHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 124)];
        view.model = self.model;
        [cell addSubview:view];
    } else {
        if (self.array) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 8, SCREEN_WIDTH - 24, (self.array.count + 1) * 30 - self.array.count - 1)];
            view.layer.masksToBounds = YES;
            view.layer.cornerRadius = 6.0;
            view.layer.borderWidth = 1;
            view.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
            for (int i = 0; i < self.array.count + 1; i++) {
                if (i == 0) {
                    StateRecordTableView *countView = [[StateRecordTableView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 30)];
                    StatusRecordModel *model = [[StatusRecordModel alloc] init];
                    model.executor = @"执行人";
                    model.content = @"执行内容";
                    model.number = @"交货数量";
                    model.time = @"时间";
                    countView.model = model;
                    [view addSubview:countView];
                } else  {
                    StateRecordTableView *countView = [[StateRecordTableView alloc] initWithFrame:CGRectMake(0, 30 * i-i-1, view.frame.size.width, 30)];
                    countView.model = [self.array objectAtIndex:i-1];
                    countView.layer.borderWidth = 1;
                    countView.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
                    [view addSubview:countView];
                }
            }
            [cell addSubview:view];
        }
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 124;
    } else  {
        return (self.array.count + 1)*30 - self.array.count - 1 + 8;
    }
}

@end
